package threading;

//定时器代码
import java.util.concurrent.PriorityBlockingQueue;

class MyTask implements Comparable<MyTask>{
    public Runnable runnable;
    //为了方便后续判定，使用绝对的时间戳
    public long time;

    public MyTask(Runnable runnable,long delay){
        this.runnable=runnable;
        //取当前时刻的时间戳 + delay，作为该任务实际执行的时间戳
        this.time=System.currentTimeMillis()+delay;
    }

    @Override
    public int compareTo(MyTask o) {
        //这样的写法意味着每次取出的是时间最小的元素
        return (int)(this.time-o.time);
    }
}

class MyTimer{
    //创建一个锁对象
    private  Object locker = new Object();
    //这个结构，带有优先级的阻塞队列，核心数据结构
    private PriorityBlockingQueue<MyTask> queue   = new PriorityBlockingQueue<>();
    //此处的 dalay 是一个形如 3000 这样的数字（多长时间后，执行该任务）
    public void schedule(Runnable runnable,long delay){
        //根据参数，构造 MyTask ，插入队列即可
        MyTask myTask = new MyTask(runnable,delay);
        queue.put(myTask);
        synchronized (locker) {
            locker.notify();
        }
    }
    //在这里构造线程，负责执行具体任务了，
    public MyTimer(){
        Thread t = new Thread(()->{
            while(true){
                    //阻塞队列，只有阻塞的入队列和阻塞的出队列，没有阻塞的查看队首元素
                    try {
                        synchronized (locker) {
                            MyTask myTask = queue.take();
                            long curTime = System.currentTimeMillis();
                            if (myTask.time <= curTime) {
                                //时间到了，可以执行任务了
                                myTask.runnable.run();
                            } else {
                                //时间还没到
                                //把刚才取出的任务，重新塞回到队列中
                                queue.put(myTask);
                                locker.wait(myTask.time-curTime);
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
        });
        t.start();
    }
}
public class ThreadDemo21 {
    public static void main(String[] args) {
        //System.out.println(System.currentTimeMillis());
        MyTimer myTimer = new MyTimer();
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello4");
            }
        },4000);
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello3");
            }
        },3000);
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello2");
            }
        },2000);
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello1");
            }
        },1000);
        System.out.println("hello0");
    }
}
